package com.springboot.mybatis.controller;

import com.springboot.mybatis.common.CommonResult;
import com.springboot.mybatis.entity.User;
import com.springboot.mybatis.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
 *DATE:2025/7/9
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/{id}")
    public User queryById(@PathVariable  Integer id){

        return userService.queryById(id);
    }

    @GetMapping("/name")
    public List<User> queryByName(String name){
        return userService.queryByName(name);
    }

    @GetMapping("/all")
    public List<User> queryAll(){
        return userService.queryAll();
    }

    @PostMapping("/add")
    public CommonResult addUser(@RequestBody User user)
    {
        userService.addUser(user);
        return new CommonResult(0,"添加成功", user);
    }

    @DeleteMapping("/{id}")
    public CommonResult deleteUserById(@PathVariable Integer id)
    {
        userService.deleteUserById(id);
        return new CommonResult(0,"删除成功", null);
    }

    @PutMapping("/update")
    public CommonResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return new CommonResult(0,"更新成功", user);
    }
}
